package Records.UserInterface;

import Records.Main.RecordsMain;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Alexander Wilson
 */
public class CheckboxChange {
static ArrayList notCheckedBoxes;
    public static void rssChartLocation() {
        System.out.println("CheckboxChange.rssChartLocation()");
        String SQLStatement = "";
        notCheckedBoxes = new ArrayList();
        int numOfCheckedBoxes = 0;
        if (RecordsMain.getUI().BillboardCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Billboard-Pop%'");
        } else {
            notCheckedBoxes.add("Billboard-Pop");
        }
        if (RecordsMain.getUI().BillboardEDMCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Billboard-EDM%'");
        } else {
            notCheckedBoxes.add("Billboard-EDM");
        }
        if (RecordsMain.getUI().BillboardCountryCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Billboard-Country%'");
        } else {
            notCheckedBoxes.add("Billboard-Country");
        }
        if (RecordsMain.getUI().BillboardRnBCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Billboard-RnB%'");
        } else {
            notCheckedBoxes.add("Billboard-RnB");
        }
        if (RecordsMain.getUI().HypeCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Hypem%'");
        } else {
            notCheckedBoxes.add("Hypem");
        }

        if (RecordsMain.getUI().ItunesCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Itunes-Pop%'");
        } else {
            notCheckedBoxes.add("Itunes-Pop");
        }
        if (RecordsMain.getUI().ItunesCountryCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Itunes-Country%'");
        } else {
            notCheckedBoxes.add("Itunes-Country");
        }
        if (RecordsMain.getUI().ItunesRnBCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Itunes-RnB%'");
        } else {
            notCheckedBoxes.add("Itunes-RnB");
        }
        if (RecordsMain.getUI().ItunesEDMCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Itunes-EDM%'");
        } else {
            notCheckedBoxes.add("Itunes-EDM");
        }
        if (RecordsMain.getUI().UKCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%UK40%'");
        } else {
            notCheckedBoxes.add("UK40");
        }
        if (RecordsMain.getUI().ShazamCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Shazam%'");
        } else {
            notCheckedBoxes.add("Shazam");
        }
        if (RecordsMain.getUI().BeatportCheckbox.isSelected()) {
            numOfCheckedBoxes++;
            SQLStatement = sqlAppend(numOfCheckedBoxes, SQLStatement, "songs.Location LIKE '%Beatport%'");
        } else {
            notCheckedBoxes.add("Beatport");
        }

        DefaultTableModel model = (DefaultTableModel) RecordsMain.getUI().rssChartsTable.getModel();
        if (numOfCheckedBoxes > 0) {
            addRows(model, SQLStatement);
        } else {
            removeRows(model);
        }
    }

    public static void removeRows(DefaultTableModel model) {
        System.out.println("CheckboxChange.removeRows(" + model + ")");
        while (model.getRowCount() > 0) {
            model.removeRow(0);
            if (RecordsMain.getUI().jTabbedPane1.getSelectedIndex() == 1) {
                RecordsMain.getUI().BasedOn.setVisible(false);
                if (model.getRowCount() == 1) {
                    RecordsMain.getUI().chartSongNum.setText(model.getRowCount() + " Song");
                } else {
                    RecordsMain.getUI().chartSongNum.setText(model.getRowCount() + " Songs");
                }
            } else {
                RecordsMain.getUI().BasedOn.setVisible(true);
                DefaultTableModel model2 = (DefaultTableModel) RecordsMain.getUI().songPrefTable.getModel();
                if (model.getRowCount() == 1) {
                    RecordsMain.getUI().chartSongNum.setText(model2.getRowCount() + " Song");
                } else {
                    RecordsMain.getUI().chartSongNum.setText(model2.getRowCount() + " Songs");
                }
            }
        }
    }

    public static void addRows(DefaultTableModel model, String SQLStatement) {
        System.out.println("CheckboxChange.removeRows(" + model + ", " + SQLStatement + ")");

        ResultSet results = RecordsMain.dba.getCharts(SQLStatement);
        removeRows(model);

        // While there are more results to process
        try {
            while (results.next()) {
                // Build a List of Strings for the table row
                List<String> songAttributes = new LinkedList<>();
                songAttributes.add(results.getString("SONG_TITLE"));
                songAttributes.add(results.getString("ARTIST"));
                songAttributes.add(results.getString("ALBUM"));
                songAttributes.add(results.getString("GENRE"));
                songAttributes.add(results.getString("BPM"));
                songAttributes.add(results.getString("THE_KEY"));
                String Location = results.getString("LOCATION");
                for(int x =0; x<notCheckedBoxes.size(); x++){
                    Location = Location.replace((notCheckedBoxes.get(x).toString()),"");
                }
                songAttributes.add(Location.trim().replace("   ", " ").replace("  ", " ").replace(" ", "/"));

                model.addRow(songAttributes.toArray());
                if (RecordsMain.getUI().jTabbedPane1.getSelectedIndex() == 1) {
                    RecordsMain.getUI().BasedOn.setVisible(false);
                    if (model.getRowCount() == 1) {
                        RecordsMain.getUI().chartSongNum.setText(model.getRowCount() + " Song");
                    } else {
                        RecordsMain.getUI().chartSongNum.setText(model.getRowCount() + " Songs");
                    }
                } else {
                    RecordsMain.getUI().BasedOn.setVisible(true);
                    DefaultTableModel model2 = (DefaultTableModel) RecordsMain.getUI().songPrefTable.getModel();
                    if (model.getRowCount() == 1) {
                        RecordsMain.getUI().chartSongNum.setText(model2.getRowCount() + " Song");
                    } else {
                        RecordsMain.getUI().chartSongNum.setText(model2.getRowCount() + " Songs");
                    }
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(CheckboxChange.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String sqlAppend(int numOfCheckedBoxes, String SQLStatement, String SQLToAdd) {
        System.out.println("CheckboxChange.sqlAppend(" + numOfCheckedBoxes + ", " + SQLStatement + ", " + SQLToAdd + ")");
        if (numOfCheckedBoxes < 2) {
            SQLStatement = SQLToAdd;
        }
        if (numOfCheckedBoxes > 1) {
            SQLStatement = SQLStatement + " OR " + SQLToAdd;
        }
        return SQLStatement;
    }
}
